package com.manor.im.utils;

import android.content.Context;
import android.os.Build;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orhanobut.logger.Logger;
import com.manor.common.CommonAppConfig;
import com.manor.common.bean.ChatInfoBean;
import com.manor.common.utils.KLog;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.ScreenUtils;
import com.tencent.imsdk.v2.V2TIMCustomElem;
import com.tencent.imsdk.v2.V2TIMMessage;
import com.tencent.qcloud.tim.uikit.http.HChatProvider;
import com.tencent.qcloud.tim.uikit.modules.message.ChatUtilInfo;

import org.jetbrains.annotations.NotNull;

/**

 * @version 1.0

 * @date 2020/7/30  16:30
 */
@Route(path = RouteUtil.IM_CHAT_PROVIDER)
public class HChatImUtil implements HChatProvider {

    @Override
    public void onChatMsg(@NotNull V2TIMMessage timMessage) {
        KLog.e("HChatImUtil#onChatMsg data", timMessage.getCustomElem());



        //接听电话的逻辑
        V2TIMCustomElem customElem = timMessage.getCustomElem();
        String data = new String(customElem.getData());
        JSONObject obj = JSON.parseObject(data);
        if (obj!=null){
            String uid = timMessage.getSender();
            ChatInfoBean bean = obj.toJavaObject(ChatInfoBean.class);
            byte action = bean.getAction();
            if (action != ChatUtilInfo.ACTION_AUD_START){
                ChatLiveImUtil.onNewMessage(obj, uid);
            }else if (Build.VERSION.SDK_INT >= 29 && !CommonAppConfig.getInstance().isFrontGround()){
                Logger.i("onChatAudToAncStart TxImMessageUtil Load Skip , isLock = "+ ScreenUtils.isScreenLock());
            }else if (Build.VERSION.SDK_INT>=29 && ScreenUtils.isScreenLock()){
                Logger.i("onChatAudToAncStart TxImMessageUtil isScreenLock Skip");
            }else {
                ChatLiveImUtil.onNewMessage(obj, uid);
            }

        }


    }

    @Override
    public void init(Context context) {

    }

}
